package com.atguigu.selector;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 * className: SelectorDemo2
 * description:
 * date: 2023/3/10 0010-11:41
 * <p>
 * project: nio
 * package: com.atguigu.selector
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class SelectorDemo2 {

    /**
     * 服务端代码
     */
    @Test
    public void serverDemo() throws IOException {
        // 1.获取服务端通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        // 2.切换非阻塞模式
        serverSocketChannel.configureBlocking(false);

        // 3.创建 buffer
        ByteBuffer serverByteBuffer = ByteBuffer.allocate(1024);

        // 4.绑定端口号
        serverSocketChannel.bind(new InetSocketAddress(8080));

        // 5.获取 selector 选择器
        Selector selector = Selector.open();

        // 6.通道注册到选择期器，进行监听
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        // 7.选择器进行轮询，进行后续操作
        while (selector.select() > 0) {
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            // 遍历
            Iterator<SelectionKey> selectionKeyIterator = selectionKeys.iterator();
            while (selectionKeyIterator.hasNext()) {
                // 获取就绪操作
                SelectionKey next = selectionKeyIterator.next();
                if (next.isAcceptable()) {
                    // 获取连接
                    SocketChannel accept = serverSocketChannel.accept();

                    // 切换非阻塞模式
                    accept.configureBlocking(false);

                    // 注册
                    accept.register(selector, SelectionKey.OP_READ);

                } else if (next.isReadable()) {
                    SocketChannel channel = (SocketChannel) next.channel();

                    // 读取数据
                    int length = 0;
                    while ((length = channel.read(serverByteBuffer)) > 0) {
                        serverByteBuffer.flip();
                        System.out.println("received : " + new String(serverByteBuffer.array()));
                        serverByteBuffer.clear();
                    }
                }
            }
            selectionKeyIterator.remove();
        }
    }

    /**
     * 客户端代码
     */
    @Test
    public void clientDemo() throws IOException {
        // 1.获取通道，绑定主机和端口号
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8080));

        // 2.切换到非阻塞模式
        socketChannel.configureBlocking(false);

        // 3.创建 buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        // 4.写入 buffer 数据
        byteBuffer.put(new Date().toString().getBytes());

        // 5.模式切换
        byteBuffer.flip();

        // 6.写入通道
        socketChannel.write(byteBuffer);

        // 7.关闭
        byteBuffer.clear();
    }

    public static void main(String[] args) throws IOException {
        // 1.获取通道，绑定主机和端口号
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8080));

        // 2.切换到非阻塞模式
        socketChannel.configureBlocking(false);

        // 3.创建 buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.next();

            // 4.写入 buffer 数据
            byteBuffer.put((new Date().toString() + "--->" + str).getBytes());

            // 5.模式切换
            byteBuffer.flip();

            // 6.写入通道
            socketChannel.write(byteBuffer);

            // 7.关闭
            byteBuffer.clear();
        }

    }
}
